package cn.ekuma.epos.panel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.dao.bean.I_ViewBean;
import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;

public class JFinderDialog<T extends I_ViewBean> extends AbstractKeyBoardJDialog {
      T selected;  
      AbstractJFinderPanel panel;
      
	   protected JFinderDialog(Frame parent, boolean modal) {
	        super(parent, modal);
	    	initComponents();
	    }

	   protected JFinderDialog(java.awt.Dialog parent, boolean modal) {
	        super(parent, modal);
	    	initComponents();
	    }  
	
	  public static JFinderDialog getInstance(Component parent, AbstractJFinderPanel panel){
	        Window window = getWindow(parent);
	        JFinderDialog myMsg;
	        if (window instanceof Frame) {
	            myMsg = new JFinderDialog((Frame) window, true);
	        } else {
	            myMsg = new JFinderDialog((Dialog) window, true);
	        }
	        myMsg.init(panel);
	        return myMsg;
	    }
	  
	  private void initComponents() {
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.SOUTH);
			panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
			
			JButton button = new JButton(AppLocal.getIntString("Button.OK"));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					enterKeyProc();
				}
			});
			panel.add(button);
			
			JButton button_1 = new JButton(AppLocal.getIntString("Button.Close"));
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					escapeKeyProc();
				}
			});
			panel.add(button_1);
			java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	        setBounds((screenSize.width-750)/2, (screenSize.height-500)/2, 750, 500);
			
		}
	  

	private void init(AbstractJFinderPanel<T> panel) {
		this.panel=panel;
		panel.jXTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	if (evt.getClickCount() == 2) 
            		enterKeyProc();
            }
        });
		getContentPane().add(panel, BorderLayout.CENTER);
	}

	@Override
	protected void enterKeyProc() {
		this.selected=(T) panel.getSelectedObj();
		dispose();
	}

	@Override
	protected void escapeKeyProc() {
		this.selected=null;
		dispose();
	}

	@Override
	protected void upKeyProc() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void downKeyProc() {
		// TODO Auto-generated method stub
	}
	
	public void query(QBFParameters qbfParameters) {
		panel.query(qbfParameters);
		this.setVisible(true);
	}

	public void query(QBFParameter para) {
		QBFParameters paras=new QBFParameters();
		paras.and(para);
		query(paras);
	}
	  
}
